package com.skcc.cop.mrtaxi.biz.destination;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.skcc.cop.mrtaxi.BaseActivity;
import com.skcc.cop.mrtaxi.R;
import com.skcc.cop.mrtaxi.util.CalendarUtil;
import com.skcc.cop.mrtaxi.view.TitleBar;
import com.skcc.cop.mrtaxi.view.TitleBar.OnTitleBarEventListener;
import com.skp.Tmap.TMapPOIItem;
import com.skp.Tmap.TMapView;



public class ModifyDestinationActivity extends BaseActivity implements OnClickListener, OnTitleBarEventListener{

	private static final String TAG = ModifyDestinationActivity.class.getSimpleName();
	private ImageButton mBackImageButton;
	private AutoCompleteTextView mAddressAutoCompleteTextView;
	private EditText mPassengerEditText;
	private Button mModifyButton;
	private String extraAddress = "";
	private String passengerNum = "";
	private int dbId = 0;
	private PlacesAutoCompleteAdapter autoCompleteAdapter;
	private TMapView mMapView = null;	
	private String destination = "";
	private Double Lati = 0.0;
	private Double Long = 0.0;
	private TitleBar mTitleBarView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_destination_modify);
		
		mTitleBarView = (TitleBar) this.findViewById(R.id.titlebar_view);
		mTitleBarView.setEventListener(this);
		mTitleBarView.showBack();
		mTitleBarView.setTitle(R.string.destination_edit);
		
		Intent intent = getIntent();
		extraAddress = intent.getExtras().getString("address");
		dbId = intent.getIntExtra("dbId", 0);

		destination = extraAddress.split(" : ")[0];
		passengerNum = extraAddress.split(" : ")[1];
		
		Toast.makeText(ModifyDestinationActivity.this, extraAddress, Toast.LENGTH_SHORT).show();
		mAddressAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.destination_modify_address1_autoCompleteTextView);
		mPassengerEditText = (EditText)findViewById(R.id.destination_modify_passenger_editText);
		
		mAddressAutoCompleteTextView.setText(destination);
		mPassengerEditText.setText(passengerNum.replace("명", "").trim());
		
		//mBackImageButton = (ImageButton)findViewById(R.id.destination_modify_back_imageButton);
		mModifyButton = (Button)findViewById(R.id.destination_modify_modify_button);
		
		mModifyButton.setOnClickListener(this);
		//mBackImageButton.setOnClickListener(this);
		
		mMapView = new TMapView(this);
		configureMapView();
		autoComplete();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.destination_modify_modify_button){

			Toast.makeText(ModifyDestinationActivity.this, "수정", Toast.LENGTH_SHORT).show();
			selectItem();
			dataBaseControl.updateDestination(makeEntity());
			finish();

		}
	}
	
	private DestinationEntity makeEntity(){
		String regTime = CalendarUtil.getTodayTimeString();
		DestinationEntity destinationEntity = new DestinationEntity();
		destinationEntity.setId(dbId);
		destinationEntity.setAddress1(destination);
		
		if(!"".equals(mPassengerEditText.getText()))
			destinationEntity.setPassenger(Integer.parseInt(mPassengerEditText.getText().toString()));
		destinationEntity.setRegTime(regTime);

//		목적지 좌표추가
		destinationEntity.setLati(Lati);
		destinationEntity.setLongi(Long);
//		목적지 Accuracy
//		destinationEntity.setAccuracy();	
		return destinationEntity;
		
	}
	
	private void autoComplete() {
		//AutoComplete
		mAddressAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.destination_modify_address1_autoCompleteTextView);
		autoCompleteAdapter  = new PlacesAutoCompleteAdapter(this, R.layout.list_item);
		mAddressAutoCompleteTextView.setAdapter(autoCompleteAdapter);
	}
	
	private void configureMapView() {

		new Thread() {
			@Override
			public void run() {		
				////SDK 인증키
				mMapView.setSKPMapApiKey("61df72e4-9a68-3b16-92ad-94c7356f5ed9");					
				mMapView.setLanguage(mMapView.LANGUAGE_KOREAN);		
			}
		}.start();

	}
	
	private void selectItem() {
		// TODO Auto-generated method stub
		
		Editable editText = mAddressAutoCompleteTextView.getText();
		String selectText = editText.toString();

		Log.i(TAG, "selectText = " + selectText);
		if(selectText.length() > 0){

			//autoCompleteAdapter.findAllPoi(selectText);
			//autoCompleteAdapter.findAddressPoi(selectText);
			TMapPOIItem item = null;

			//TMapPOIItem 이거를 STring으로 안받는 이유가.. TMapPOIItem안에 map으로 이루어져있음.
			ArrayList<TMapPOIItem> tempList = new ArrayList<TMapPOIItem>();
			tempList = autoCompleteAdapter.gettMapList();

			if(tempList != null){

				//for문 돌려서 autoComplete에 있는 목록 뽑아와서 내가 선택한 명칭과 비교..
				for(int i=0; i < tempList.size(); i++){			
					TMapPOIItem tempItem = tempList.get(i);
					if(selectText.equals(tempItem.name)){				
						item = tempItem;
						Log.i(TAG, "Lati = " + item.getPOIPoint().getLatitude());
						Log.i(TAG, "Long = " + item.getPOIPoint().getLongitude());
						break;
					}
				}

				try{
					//명칭
					destination = item.name;
					//위도
					Lati = item.getPOIPoint().getLatitude();
					//경도
					Long = item.getPOIPoint().getLongitude();


				}catch(NullPointerException e){

					Toast.makeText(ModifyDestinationActivity.this, "찾고자 하는 주소가 없습니다.", Toast.LENGTH_SHORT).show();
					e.getStackTrace();
				}
			}

		}else{

			Toast.makeText(ModifyDestinationActivity.this, "찾고자 하는 주소가 없습니다.", Toast.LENGTH_SHORT).show();

		}
	}

	@Override
	public void onBackClicked() {
		// TODO Auto-generated method stub
		
		finish();
		
	}

	@Override
	public void onAddClicked() {
		// TODO Auto-generated method stub
		
	}
	
	
}